package com.peanut.baby.mvp.search;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.peanut.baby.InitManager;
import com.peanut.baby.R;
import com.peanut.baby.model.BBSMoment;
import com.peanut.baby.model.LiveRoom;
import com.peanut.baby.model.QA;
import com.peanut.baby.model.SearchResultItem;
import com.peanut.baby.mvp.bbs.detail.BBSMomentDetailActivity;
import com.peanut.baby.mvp.livedetail.LiveDetailActivity;
import com.peanut.baby.mvp.profile.ProfileActivity;
import com.peanut.baby.mvp.qadetail.QADetailActivity;
import com.peanut.baby.util.TimeUtil;
import com.peanut.devlibrary.adapter.HolderAdapter;
import com.peanut.devlibrary.imageloader.ImageLoader;
import com.peanut.devlibrary.util.StringUtil;
import com.tencent.mars.xlog.Log;

import java.util.List;

/**
 * Created by anonymouslystudio on 2018/6/20.
 */

public class SearchResultAdapter extends HolderAdapter<SearchResultItem,SearchResultAdapter.ViewHolder> {
    private OnViewClickedListener listener;
    public SearchResultAdapter(Context context, List<SearchResultItem> listData) {
        super(context, listData);
    }

    public void setOnViewClickListener(OnViewClickedListener listener){
        this.listener = listener;
    }

    @Override
    public View buildConvertView(LayoutInflater layoutInflater, SearchResultItem searchResultItem, int position) {
        int viewType = getItemViewType(position);
        View v = null;
        switch (viewType){
            case SearchResultItem.ITEM_LABEL:
                v = layoutInflater.inflate(R.layout.layout_item_search_label,null);
                break;
            case SearchResultItem.ITEM_QA_MORE:
            case SearchResultItem.ITEM_BBS_MORE:
            case SearchResultItem.ITEM_LIVE_MORE:
                v = layoutInflater.inflate(R.layout.layout_item_search_more,null);
                break;
            case SearchResultItem.ITEM_QA:
                v = layoutInflater.inflate(R.layout.layout_item_qa_list_pure,null);
                break;
            case SearchResultItem.ITEM_BBS:
                v = layoutInflater.inflate(R.layout.layout_item_bbs_moment_pure,null);
                break;
            case SearchResultItem.ITEM_LIVE:
                v = layoutInflater.inflate(R.layout.layout_item_live_list,null);
                break;
            default:
                v = layoutInflater.inflate(R.layout.layout_item_search_label,null);
                break;
        }
        return v;
    }

    @Override
    public ViewHolder buildHolder(View convertView, SearchResultItem searchResultItem, int position) {
        ViewHolder holder = new ViewHolder();
        holder.parentView = convertView;
        return holder;
    }

    @Override
    public void bindViewDatas(ViewHolder holder, SearchResultItem searchResultItem, int position) {
        int type = searchResultItem.type;
        Log.d("TAG","position: "+position+" type "+searchResultItem.type);
        switch (type){
            case SearchResultItem.ITEM_LABEL:
                bindLabel(position,searchResultItem,holder);
                break;
            case SearchResultItem.ITEM_QA_MORE:
            case SearchResultItem.ITEM_BBS_MORE:
            case SearchResultItem.ITEM_LIVE_MORE:
                bindMore(position,searchResultItem,holder);
                break;
            case SearchResultItem.ITEM_QA:
                bindQA(position,searchResultItem,holder);
                break;
            case SearchResultItem.ITEM_BBS:
                bindBBS(position,searchResultItem,holder);
                break;
            case SearchResultItem.ITEM_LIVE:
                bindLive(position,searchResultItem,holder);
                break;
        }
    }

    /**
     *
     */
    private void bindLabel(int position,SearchResultItem item,ViewHolder holder){
        TextView label = (TextView) holder.parentView.findViewById(R.id.result_label);
        label.setText((String) item.data);
    }

    private void bindMore(final int position, final SearchResultItem item, ViewHolder holder){
        Log.d("TAG,","bindMore "+item.data.toString());
        TextView label = (TextView) holder.parentView.findViewById(R.id.result_more);
        label.setText((String) item.data);
        label.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null){
                    listener.onItemClick(position,item);
                }
            }
        });
    }

    private void bindBBS(int position,SearchResultItem item,ViewHolder holder){
        ImageView avatar = (ImageView) holder.parentView.findViewById(R.id.moment_avatar);
        ImageView hasImage = (ImageView) holder.parentView.findViewById(R.id.moment_image_flag);
        TextView nick = (TextView) holder.parentView.findViewById(R.id.moment_nick);
        TextView time = (TextView) holder.parentView.findViewById(R.id.moment_time);
        TextView category = (TextView) holder.parentView.findViewById(R.id.moment_category);
        TextView content = (TextView) holder.parentView.findViewById(R.id.moment_content);
        TextView scanCount = (TextView) holder.parentView.findViewById(R.id.moment_scan_count);
        TextView cmtCount = (TextView) holder.parentView.findViewById(R.id.moment_comment_count);


        category.setVisibility(View.GONE);

        final BBSMoment moment = (BBSMoment) item.data;

        ImageLoader.getInstance().loadImageView(context,moment.avatar,avatar);
        content.setText(moment.title);
        nick.setText(moment.nickname);
        time.setText(TimeUtil.getDisplayTimeMillis(moment.updateTime));
        hasImage.setVisibility(StringUtil.isNullOrEmpty(moment.imgUrl)? View.GONE : View.VISIBLE);
        category.setText(moment.type);
        scanCount.setText(moment.viewCount+"");
        cmtCount.setText(moment.commentCount+"");

        holder.parentView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BBSMomentDetailActivity.start(context,moment.id+"");
            }
        });

        avatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ProfileActivity.start(context,moment.userId+"");
            }
        });
    }

    private void bindQA(final int position, final SearchResultItem item, ViewHolder holder){
        TextView   nick = (TextView) holder.parentView.findViewById(R.id.qa_asker_nick);
        ImageView  avatar = (ImageView) holder.parentView.findViewById(R.id.qa_asker_avatar);
        TextView   date = (TextView) holder.parentView.findViewById(R.id.qa_asker_date);
        TextView   status = (TextView) holder.parentView.findViewById(R.id.qa_asker_status);
        TextView   lisCount = (TextView) holder.parentView.findViewById(R.id.qa_listen_count);
        TextView   cmtCount = (TextView) holder.parentView.findViewById(R.id.qa_cmt_count);
        final CheckBox prasiedCount = (CheckBox) holder.parentView.findViewById(R.id.qa_prasied_count);
        TextView   content = (TextView) holder.parentView.findViewById(R.id.qa_content);
        TextView   category = (TextView) holder.parentView.findViewById(R.id.qa_category);

        category.setVisibility(View.GONE);

        final QA qa = (QA) item.data;

        ImageLoader.getInstance().loadImageView(context,qa.creatorAvatar,avatar);
        nick.setText(qa.creatorNickname);
        date.setText(TimeUtil.getQAListDisplayTime(qa.createTime));
        status.setText(qa.getStatusString());
        lisCount.setText(qa.listenCount+"");
        cmtCount.setText(qa.commentCount+"");
        prasiedCount.setText(qa.praiseCount+"");
        content.setText(qa.description);
        category.setText(qa.type);
        prasiedCount.setChecked(qa.isPraised());

        if(qa.isAnswered()){
            status.setTextColor(context.getResources().getColor(R.color.colorPrimary));
        }else{
            status.setTextColor(context.getResources().getColor(R.color.text_dark));
        }

        holder.parentView.findViewById(R.id.qa_container).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                QADetailActivity.start(context,qa.id);
            }
        });

        prasiedCount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                prasiedCount.setChecked(qa.isPraised());

                if(listener != null){
                    listener.onPraiseClick(position,item);
                }
            }
        });

        avatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ProfileActivity.start(context,qa.creatorId+"");
            }
        });

    }

    private void bindLive(final int position,final SearchResultItem item,ViewHolder holder){
        TextView title = (TextView) holder.parentView.findViewById(R.id.live_title);
        TextView expertName = (TextView) holder.parentView.findViewById(R.id.live_expert_name);
        TextView expertTitle = (TextView) holder.parentView.findViewById(R.id.live_expert_title);
        TextView expertHospital = (TextView) holder.parentView.findViewById(R.id.live_expert_hospital);
        TextView status = (TextView) holder.parentView.findViewById(R.id.live_status);
        TextView time = (TextView) holder.parentView.findViewById(R.id.live_time);
        TextView enroled = (TextView) holder.parentView.findViewById(R.id.live_enroled);
        TextView enroleCount = (TextView) holder.parentView.findViewById(R.id.live_enrole_count);
        ImageView image = (ImageView)  holder.parentView.findViewById(R.id.live_image);
        TextView price = (TextView) holder.parentView.findViewById(R.id.live_price);
        TextView join = (TextView) holder.parentView.findViewById(R.id.live_join);

        final LiveRoom room = (LiveRoom) item.data;

        title.setText(room.title);
        expertName.setText(room.expertName);
        expertHospital.setText(room.expertHospital);
        expertTitle.setText(room.expertJobTitle);
        time.setText(room.getTimeString());
        status.setText(room.getStatusString());
        enroled.setText("已报名"+room.enrolCount);
        enroleCount.setText("/"+room.allowCount);
        price.setText(room.getPriceString());

        if(room.expertId.equals(InitManager.getInstance().getUserId())
                || room.serverId.equals(InitManager.getInstance().getUserId())){
            join.setText("我的直播");
        } else {
            if(room.isEnroled == 0){
                join.setText("立即报名");
            }else{
                if(room.roomStatus == LiveRoom.ROOM_STATUS_FINISHED){
                    join.setText("回放中");
                }else{
                    join.setText("进入直播间");
                }

            }
        }

        ImageLoader.getInstance().loadImageView(title.getContext(),room.roomImageUrl,image);

        holder.parentView.findViewById(R.id.live_container).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LiveDetailActivity.start(context,room);
            }
        });

        join.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null){
                    listener.onJoinClick(position,item);
                }
            }
        });
    }


    @Override
    public int getItemViewType(int position) {
        return listData.get(position).type;
    }

    @Override
    public int getViewTypeCount() {
        return 7;
    }

    class ViewHolder{
        View parentView;
    }

    interface OnViewClickedListener{
        void onPraiseClick(int position, SearchResultItem item);
        void onJoinClick(int position, SearchResultItem item);
        void onItemClick(int position, SearchResultItem item);
    }
}
